﻿package  
{
	//AIR drag
	//import flash.desktop.Clipboard;
	//import flash.desktop.ClipboardFormats;
	//import flash.desktop.NativeApplication;
	//import flash.desktop.NativeDragActions;
	//import flash.desktop.NativeDragManager;
	//import flash.events.NativeDragEvent;
	
	//AIR Stage
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.errors.MemoryError;
	//import flash.display.NativeWindowResize; 
	
	//file IO
	import flash.filesystem.File;
	import flash.filesystem.FileStream;
	import flash.filesystem.FileMode;
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	
	//mouse cursor
	import flash.ui.Mouse;
	
	//events
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.events.SliderEvent;

	//display
	import flash.display.Sprite;
	import flash.display.Loader;
	import idv.etrex.display.object.DynamicMask;
	
	/**
	 * ...
	 * @author etrex
	 */
	public class DynamicMaskTool extends Sprite
	{
		var dm:DynamicMask = new DynamicMask();
		var loader1:Loader = new Loader();
		var loader2:Loader = new Loader();
		var sensor:Sprite = new Sprite();
		var selectedStrokeSize:uint = 10;
		
		public function DynamicMaskTool() {
			super();
			addEventListener(Event.ADDED_TO_STAGE, onStaged);
		}
		
		private function onStaged(event:Event):void {
			//stage mode
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.stageFocusRect = false;
			
			//initialize display object
			background.alpha = 0.1;
			sensor.x = dm.x = background.x;
			sensor.y = dm.y = background.y;
			background.mouseChildren = dm.mouseChildren = false;
			background.mouseEnabled = dm.mouseEnabled  = cursor.mouseEnabled = false;
			this.addChild(dm);
			this.addChild(sensor);
			this.removeChild(cursor);
			this.addChild(cursor);
			
			//xgraphics record
			dm.recordMode = true;
			dm.graphicsBat.lineStyle(10, 0);
			
			//stroke size view
			cursor.scaleX = cursor.scaleY = strokeSizeViewer.scaleX = strokeSizeViewer.scaleY = selectedStrokeSize / 100;
			sliderLabel.text = "strokeSize: " + selectedStrokeSize;
			
			//button event
			saveXGraphicsButton.addEventListener(MouseEvent.CLICK, saveXGraphicsButtonClick);
			loadFileButton.addEventListener(MouseEvent.CLICK, loadFileButtonClick);
			
			//loader event
			loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete1);
			loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete2);
			
			//mouse Event
			sensor.addEventListener(MouseEvent.MOUSE_DOWN, sensorMouseDown);
			sensor.addEventListener(MouseEvent.MOUSE_UP, sensorMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, cursorMouseMove);
			
			//slider Event
			slider.addEventListener(SliderEvent.CHANGE, sliderChanged);
			
			//AIR drag
			//tag.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
			//tag.addEventListener(NativeDragEvent.NATIVE_DRAG_OVER,onDragOver);
            //tag.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
            //tag.addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT,onDragExit);
		}
		
		
		//save Button Event
		private function saveXGraphicsButtonClick(event:MouseEvent):void {
			trace("saveXGraphicsButtonClick");
			var docsDir:File = new File();
			try
			{
				docsDir.browseForSave("Save As");
				docsDir.addEventListener(Event.SELECT, saveData);
			}
			catch (error:Error)
			{
				trace("Failed:", error.message);
			}
			
			function saveData(event:Event):void 
			{
				var newFile:File = event.target as File;
				var str:String = dm.xgraphics.toString();
				//if (!newFile.exists)
				{
					var stream:FileStream = new FileStream();
					stream.open(newFile, FileMode.WRITE);
					stream.writeUTFBytes(str);
					stream.close();
				}
			}
		}
		
		//load File Button Event
		private function loadFileButtonClick(event:MouseEvent):void {
			trace("loadFileButtonClick");
			var docsDir:File = new File();
			
			docsDir.browseForOpen("open!");
			docsDir.addEventListener(Event.SELECT, fileSelected);
			function fileSelected(event:Event):void {
				trace(docsDir.nativePath);
				loader1.load(new URLRequest(docsDir.nativePath));
				loader2.load(new URLRequest(docsDir.nativePath));
			}
		}
		
		//load data Event
		private function loaderComplete1(event:Event):void{
			trace("loaderComplete");
			dm.loadMasked(loader1.content);
		}
		private function loaderComplete2(event:Event):void{
			trace("loaderComplete");
			background.addChild(loader2.content);
			sensor.graphics.clear();
			sensor.graphics.beginFill(0, 0);
			sensor.graphics.drawRect(0, 0, background.width, background.height);
			sensor.graphics.endFill();
			this.stage.nativeWindow.width = sensor.x + sensor.width + 50; 
			this.stage.nativeWindow.height = sensor.y + sensor.height + 50; 
			
		}
		
		//Mouse Event
		//sensor.addEventListener(MouseEvent.MOUSE_DOWN, sensorMouseDown);
		private function sensorMouseDown(e:MouseEvent):void {
			Mouse.hide();
			dm.graphicsBat.moveTo(sensor.mouseX,sensor.mouseY);			
			sensor.addEventListener(MouseEvent.MOUSE_MOVE, sensorMouseMove);
		}
		private function sensorMouseMove(e:MouseEvent):void {
			dm.graphicsBat.lineTo(sensor.mouseX,sensor.mouseY);
		}
		private function sensorMouseUp(e:MouseEvent):void {
			Mouse.show();
			sensor.removeEventListener(MouseEvent.MOUSE_MOVE, sensorMouseMove);
		}
		
		private function cursorMouseMove(e:MouseEvent):void {
			cursor.x = e.stageX;
			cursor.y = e.stageY;
		}
		
		//slider Event : select stroke size
		public function sliderChanged(e:SliderEvent):void {
            selectedStrokeSize = e.target.value+1;
			cursor.scaleX = cursor.scaleY = strokeSizeViewer.scaleX = strokeSizeViewer.scaleY = selectedStrokeSize / 100;
			sliderLabel.text = "strokeSize: " + selectedStrokeSize;
			dm.graphicsBat.lineStyle(selectedStrokeSize,0);
        }
		
		//AIR drag
		//private function onDragIn(event:NativeDragEvent):void{
            //trace("Drag enter event.");
            //var transferable:Clipboard = event.clipboard;
                        //
            //if(transferable.hasFormat(ClipboardFormats.BITMAP_FORMAT) ||
               //transferable.hasFormat(ClipboardFormats.FILE_LIST_FORMAT) ||
               //transferable.hasFormat(ClipboardFormats.TEXT_FORMAT) ||
               //transferable.hasFormat(ClipboardFormats.URL_FORMAT) ||
               //transferable.hasFormat(Scrap.SCRAP_FORMAT)
			   //){
                   //NativeDragManager.acceptDragDrop(this);
	               //NativeDragManager.dropAction = NativeDragActions.MOVE;
            //} else {
            	//trace("Unrecognized format");
            //}     
        //}
		//
		//private function onDragOver(event:NativeDragEvent):void{
        	//if(event.clipboard.hasFormat(Scrap.SCRAP_FORMAT)){
        		//if(event.ctrlKey || event.controlKey){
        			//NativeDragManager.dropAction = NativeDragActions.COPY;
        		//} else {
        			//NativeDragManager.dropAction = NativeDragActions.MOVE;
        		//}	
        	//}
        //}
        //
		//private function onDrop(event:NativeDragEvent):void{
        	//trace("Drag drop event.");
            //var scrap:Scrap = Scrap.createScrap(event.clipboard, event.stageX, event.stageY, event.dropAction);
            //dragTarget.addChild(scrap);
        //}
        //
		//private function onDragExit(event:NativeDragEvent):void{
            //trace("Drag exit event.");
        //}
        
	}
}